package interfaces.hud.pilot;

import java.util.LinkedList;

import org.fenggui.ScrollBar;
import org.fenggui.ScrollContainer;
import org.fenggui.appearance.LabelAppearance;
import org.fenggui.binding.render.ImageFont;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.decorator.border.PlainBorder;
import org.fenggui.layout.StaticLayout;
import org.fenggui.util.Color;

import logic.common.player.Player;
import logic.common.team.Team;
import logic.common.team.processes.ProcessAddEvent;
import logic.common.team.processes.ProcessFinishEvent;
import logic.common.team.processes.ProcessListener;
import logic.common.team.processes.ProcessStartEvent;
import logic.common.team.processes.ProcessUpdateEvent;
import logic.common.team.processes.TeamProcess;
import logic.common.team.processes.TeamProcessListener;
import logic.common.team.teamListeners.DroneKillEvent;
import logic.common.team.teamListeners.TeamListener;
import logic.common.team.teamListeners.TeamValueChangedEvent;
import logic.common.team.teamListeners.TeamValueType;

import fileHandling.language.Lang;
import fileHandling.language.LanguageLoader;
import fileHandling.language.interfaces.HUDText;
import interfaces.GUISource;
import interfaces.PropertyContent;
import interfaces.hud.BasicHUD;
import interfaces.superWidgets.InterfaceLabel;
import interfaces.superWidgets.MinSizeWindow;
import interfaces.superWidgets.StaticContent;

public class TeamStatusWindow extends MinSizeWindow {
	
	private Team team;
	private PropertyContent progressContent;
	private ScrollContainer scrollContainer;
	private StaticContent listContent;
	private ImageFont labelFont;
	private int labelHeight;
	private ProcessListener processListener;
	private TeamProcess activeProcess;
	
	public TeamStatusWindow(BasicHUD hud, Team team, int width, int height, int y) {
		super(HUDText.Team_Money, Mode.UP, hud, width, height, y);
		this.team = team;
		
		setMinimized(true);
		
		init();
		
		if(!team.getPendingTeamProcesses().isEmpty()) updateTeamProcesses();
		
		TeamProcess activeProcess = team.getActiveTeamProcess();
		if(activeProcess != null) {
			activeProcess.addProcessListener(processListener);
			progressContent.setValue(activeProcess.getPercentage());
		}
		
		hud.addListener(team, new TeamListener() {
			@Override
			public void teamValueChanged(TeamValueChangedEvent event) {
				if(event.getType() == TeamValueType.Money) {
					setMinimized(isMinimized);
				}
			}
			
			@Override
			public void playerRemoved(Player player) {}
			@Override
			public void playerAdded(Player player) {}
			@Override
			public void droneDestroyed(DroneKillEvent event) {}
		});
		
		layout();
	}
	
	private void init() {
		content.setLayoutManager(new StaticLayout());
		
		int width = content.getWidth();
		int propHeight = GUISource.middleFont.getHeight();
		int propY = content.getHeight() - propHeight;
		
		interfaces.PropertyContent.Mode mode = interfaces.PropertyContent.Mode.LEFT;
		Lang init = HUDText.Drone;
		progressContent = new PropertyContent(width, propHeight, 0, propY, init, mode);
		progressContent.setMaxValue(100f);
		progressContent.setValue(0f);
		
		int height = propY;
		labelFont = GUISource.middleFont;
		labelHeight = labelFont.getHeight();
		
		scrollContainer = GUISource.createScrollContainer();
		scrollContainer.setMinSize(width, height);
		scrollContainer.setSizeToMinSize();
		scrollContainer.setShowScrollbars(true);
		content.addWidget(scrollContainer);
		layout();
		
		ScrollBar horScrollBar = scrollContainer.getHorizontalScrollBar();
		ScrollBar verScrollBar = scrollContainer.getVerticalScrollBar();
		
		horScrollBar.setMinSize(0, 0);
		horScrollBar.setSizeToMinSize();
		horScrollBar.setVisible(false);
		horScrollBar.setEnabled(false);
		
		LabelAppearance buttonApp = verScrollBar.getSlider().getSliderButton().getAppearance();
		buttonApp.add(new PlainBorder(Color.BLUE));
		buttonApp.add(new PlainBackground(Color.BLUE));
		
		listContent = new StaticContent(width, height, 0, 0);
		listContent.setMinSize(width, height);
		scrollContainer.setInnerWidget(listContent);
		
		hud.addListener(team, new TeamProcessListener() {
			@Override
			public void processAdded(ProcessAddEvent event) {
				updateTeamProcesses();
			}
			
			@Override
			public void processStarted(ProcessStartEvent event) {
				updateTeamProcesses();
				setActiveProcess(event.getTeamProcess());
			}
			
			@Override
			public void processFinished(ProcessFinishEvent event) {
				if(team.getActiveTeamProcess() == null) setActiveProcess(null);
			}
		});
		
		processListener = new ProcessListener() {
			@Override
			public void processUpdated(ProcessUpdateEvent event) {
				progressContent.setValue(event.getPercentage());
			}
		};
	}
	
	private void setActiveProcess(TeamProcess p) {
		if(activeProcess != null) activeProcess.removeProcessListener(processListener);
		
		activeProcess = p;
		if(activeProcess != null) {
			content.addWidget(progressContent);
			activeProcess.addProcessListener(processListener);
			progressContent.setText(activeProcess.getDescription());
		} else content.removeWidget(progressContent);
		
		progressContent.setValue(0f);
	}
	
	private void updateTeamProcesses() {
		listContent.removeAllWidgets();
		
		LinkedList<TeamProcess> processes = team.getPendingTeamProcesses();
		int newHeight = labelHeight * processes.size();
		if(newHeight < scrollContainer.getHeight()) newHeight = scrollContainer.getHeight();
		
		for(int i = 0; i < processes.size(); i++) {
			TeamProcess p = processes.get(i);
			
			String text = LanguageLoader.get(p.getDescription());
			InterfaceLabel label = new InterfaceLabel(text, labelFont);
			label.setSize(listContent.getWidth(), labelHeight);
			label.setY(newHeight - ((i + 1) * labelHeight));
			listContent.addWidget(label);
		}
		
		listContent.setMinSize(scrollContainer.getWidth(), newHeight);
		listContent.setSizeToMinSize();
		listContent.setY(0);
		
		layout();
	}
	
	@Override
	public String getMinimizedTitle() {
		return (int)team.getTeamMoney() + "";
	}
	
	@Override
	public String getMaximizedTitle() {
		return LanguageLoader.get(HUDText.Team_Money) + ": " + (int)team.getTeamMoney();
	}
}